package com.googlecode.c4e.ui.views.internal;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.progress.IDeferredWorkbenchAdapter;

import com.googlecode.c4e.api.model.Project;
import com.googlecode.c4e.api.model.ProjectGroup;
import com.googlecode.c4e.api.model.Server;

public class C4EAdapterFactory implements IAdapterFactory {

	private Map<Class, Object> adapters = initializeAdapters();
	private Map<Class, Object> deferredAdapters = initializeDeferredAdapters();

	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (adapterType == IDeferredWorkbenchAdapter.class) {
			return deferredAdapters.get(adaptableObject.getClass());
		}
		return adapters.get(adaptableObject.getClass());
	}

	private Map<Class, Object> initializeAdapters() {
		Map<Class, Object> result = new HashMap<Class, Object>();
		result.put(Project.class, new ProjectAdapter());
		result.put(Server.class, new ServerAdapter());
		result.put(ProjectGroup.class, new ProjectGroupAdapter());
		return Collections.unmodifiableMap(result);
	}

	private Map<Class, Object> initializeDeferredAdapters() {
		Map<Class, Object> result = new HashMap<Class, Object>();
		result.put(ProjectGroup.class, new ProjectGroupAdapter());
		result.put(Server.class, new ServerAdapter());
		return Collections.unmodifiableMap(result);
	}

	public Class[] getAdapterList() {
		return new Class[] { IWorkbenchAdapter.class,
				IDeferredWorkbenchAdapter.class };
	}

}
